package com.gateway.security.config;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/10 23:20
 **/
public class SecurityConfig {

    public static final SecurityConfig INSTANCE = new SecurityConfig();

    public Map<String, Config> configMap;

    public Map<String, Config> getConfigMap() {
        return configMap == null ? Maps.newHashMap() : ImmutableMap.<String, Config>builder().putAll(configMap).build();
    }

    public void setConfigMap(Map<String, Config> configMap) {
        this.configMap = configMap;
    }

    public static class Config {

        /**
         * 是否开启缓存
         */
        private boolean cached;

        /**
         * 是否自动刷新
         */
        private boolean autoRefresh;

        /**
         * 最大容量
         */
        private Integer maximumSize = 1000;

        /**
         * 缓存过期时间
         */
        private Integer expireAfter;

        /**
         * 时间单位
         */
        private String unit;

        /**
         * 自动刷新时间
         */
        private Integer autoRefreshAfter;

        /**
         * 自动刷新时间单位
         */
        private String autoRefreshUnit;

        /**
         * 请求地址
         */
        private String uri;

        /**
         * token传入uri字段名
         */
        private String tokenName;

        /**
         * 获取token位置
         * header/cookie/query
         */
        private String paramType;


        /**
         * token参数名称
         */
        private String paramName;


        /**
         * 请求参数
         */
        private Map<String, String> queryParams;

        /**
         * 超时时间
         */
        private Long timeout = 3000L;

        public String getTokenName() {
            return StringUtils.defaultIfEmpty(tokenName, "token");
        }

        public String getUri() {
            return uri;
        }

        public void setUri(String uri) {
            this.uri = uri;
        }

        public void setTokenName(String tokenName) {
            this.tokenName = tokenName;
        }

        public Map<String, String> getQueryParams() {
            return queryParams;
        }

        public void setQueryParams(Map<String, String> queryParams) {
            this.queryParams = queryParams;
        }

        public boolean isCached() {
            return cached;
        }

        public void setCached(boolean cached) {
            this.cached = cached;
        }

        public Integer getMaximumSize() {
            return maximumSize < 1000 ? 1000 : maximumSize;
        }

        public void setMaximumSize(Integer maximumSize) {
            this.maximumSize = maximumSize;
        }

        public Integer getExpireAfter() {
            return expireAfter;
        }

        public void setExpireAfter(Integer expireAfter) {
            this.expireAfter = expireAfter;
        }

        public String getUnit() {
            return unit;
        }

        public void setUnit(String unit) {
            this.unit = unit;
        }

        public boolean isAutoRefresh() {
            return autoRefresh;
        }

        public void setAutoRefresh(boolean autoRefresh) {
            this.autoRefresh = autoRefresh;
        }

        public Integer getAutoRefreshAfter() {
            return autoRefreshAfter;
        }

        public void setAutoRefreshAfter(Integer autoRefreshAfter) {
            this.autoRefreshAfter = autoRefreshAfter;
        }

        public String getAutoRefreshUnit() {
            return autoRefreshUnit;
        }

        public void setAutoRefreshUnit(String autoRefreshUnit) {
            this.autoRefreshUnit = autoRefreshUnit;
        }

        public Long getTimeout() {
            return timeout;
        }

        public void setTimeout(Long timeout) {
            this.timeout = timeout;
        }

        public String getParamType() {
            return paramType;
        }

        public void setParamType(String paramType) {
            this.paramType = paramType;
        }

        public String getParamName() {
            return paramName;
        }

        public void setParamName(String paramName) {
            this.paramName = paramName;
        }
    }
}
